/*
 * @Author: 葛文杰
 * @Date: 2021/2/1 17:50
 * @Description: egg controller 获取数据源配置
 */

'use strict';

const BaseController = require('./base');
const { PULL_TIME_OPTION_ARRAY } = require('../util/pullTimeOptions');


class DataSourceConfigController extends BaseController {

    /**
     * 获取数据源的获取数据频率
     */
    async getDataSourcePullRateArray() {
        this.success({ data: PULL_TIME_OPTION_ARRAY });
    }

    /**
     * 获取数据源配置列表
     */
    async getDataSourceConfigList() {
        const { ctx, service } = this;
        const { body: dataSourceConfigCondition } = ctx.request;
        let tableData =
          await service.dataSourceConfig.getDataSourceConfigList(dataSourceConfigCondition);
        this.success({ data: tableData });
    }

    /**
     * 获取数据源配置
     * @return {Promise<void>}
     */
    async getDataSourceConfigById() {
        const { ctx, service } = this;
        let { body: dataSourceConfigCondition } = ctx.request;
        let result = await service.dataSourceConfig.getDataSourceConfigById(dataSourceConfigCondition);
        this.success({ data: result });
    }

    /**
     * 存储数据源配置
     * @return {Promise<void>}
     */
    async insertDataSourceConfigInfo() {
        const { ctx, service } = this;
        const {
            body: dataSourceConfigCondition
        } = ctx.request;
        let result = await service.dataSourceConfig.insertDataSourceConfigInfo(dataSourceConfigCondition);
        this.success({ data: result });
    }

    /**
     * 更新数据源配置
     * @return {Promise<void>}
     */
    async updateDataSourceConfig() {
        const { ctx, service } = this;
        const {
            body: dataSourceConfigCondition
        } = ctx.request;
        let result = await service.dataSourceConfig.updateDataSourceConfig(dataSourceConfigCondition);
        this.success({ data: result });
    }

    /**
     * 逻辑删除数据源配置
     * @return {Promise<void>}
     */
    async deleteDataSourceConfigById() {
        const { ctx, service } = this;
        let { dataSourceConfigId } = ctx.request.body;
        let result = await service.dataSourceConfig.deleteDataSourceConfigById(dataSourceConfigId);
        this.success({ data: result });
    }

    /**
     * 启/停用数据源配置
     * @return {Promise<void>}
     */
    async changeDataSourceConfigEnable() {
        const { ctx, service } = this;
        let { body: dataSourceConfigCondition } = ctx.request;
        let result = await service.dataSourceConfig.changeDataSourceConfigEnable(dataSourceConfigCondition);
        this.success({ data: result });
    }

}

module.exports = DataSourceConfigController;
